package com.sg.file.receive.provider.service;

import com.sg.common.model.ResultCode;
import com.sg.common.model.ResultModel;
import com.sg.common.utils.ResultModelUtils;
import com.sg.file.common.conf.FileConfiguration;
import com.sg.file.manager.api.IFileInfoService;
import com.sg.file.manager.repository.pojo.FileInfo;
import com.sg.file.receive.api.IFileManagerService;
import com.sg.file.receive.api.dto.FileCreateDTO;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FilenameFilter;
import java.util.List;

/**
 * @Author xzk
 * @Date 2019/1/1 23:32
 * @Version 1.0
 **/
@Service("fileManagerService")
public class FileManagerService implements IFileManagerService {
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private FileConfiguration fileConfiguration;

    @Override
    public ResultModel delete(String fileId) {
        boolean succ = false;
        String message = "";
        //数据库中查询出对应的文件
        FileInfo fileInfo = fileInfoService.getById(fileId);
        //获取源文件的路径
        String filePath = fileInfo.getFilePath();
        filePath = StringUtils.substring(filePath,0,filePath.lastIndexOf(File.separator));
        String basePath = StringUtils.substring(filePath,0,filePath.lastIndexOf(File.separator));
        File basePathFile = new File(basePath);
        File[] files = basePathFile.listFiles();
        try {
            if (ArrayUtils.isNotEmpty(files)) {
                for (File directory : files) {
                    if (directory.isDirectory()) {
                        File[] childFiles = directory.listFiles();
                        if (ArrayUtils.isNotEmpty(childFiles)) {
                            for (File file : childFiles) {
                                if (file.isFile()) {
                                    String fileName = StringUtils.substring(file.getName(), 0, file.getName().lastIndexOf("."));
                                    if (fileName.equals(fileId)) {
                                        file.delete();
                                    }
                                }
                            }
                        }
                    }
                }
            }
//        if(fileInfo!=null&& StringUtils.isNotEmpty(fileInfo.getFilePath())){
//            File file = new File(fileInfo.getFilePath());
//            //TODO
//            //文件删除未删除干净需要进行全路径删除
//            if(file.exists()){
//                try {
//                    file.delete();
//                    fileInfoService.delete(fileId);
//                    succ = true;
//                }catch (Exception e){
//                    message = e.getMessage();
//                }finally {
//                    return ResultModelUtils.getInstance(succ, ResultCode.OPERATION_ERROR.toString(),message);
//                }
//            }else{
//                return ResultModelUtils.getFailInstance(ResultCode.PARAM_NULL.toString(),"无此文件");
//            }
//        }else{
//            return ResultModelUtils.getFailInstance(ResultCode.PARAM_NULL.toString(),"无此文件");
//        }
            fileInfoService.delete(fileId);
            succ = true;
        }catch (Exception e){
            message = e.getMessage();
        }finally {
            if(succ){
                return ResultModelUtils.getSuccessInstance();
            }else {
                return ResultModelUtils.getInstance(succ, ResultCode.OPERATION_ERROR.toString(), message);
            }
        }
    }



    /**
     * 删除原文件
     * @param delFiles
     */
    private void delOriginFile(List<FileCreateDTO> delFiles) {

    }
}
